package com.bzkj.basicinfo.service.impl;

import com.bzkj.basicinfo.domain.wms.WmsBillsTypeBillsStatusEntity;
import com.bzkj.basicinfo.domain.wms.vo.WmsBillsTypeBillsStatusTreeVo;
import com.bzkj.basicinfo.mapper.WmsBillsTypeBillsStatusMapper;
import com.bzkj.basicinfo.service.WmsBillsTypeBillsStatusService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.common.core.domain.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author wl
 * @description 针对表【wms_bills_type_bills_status(单据状态类型关联表)】的数据库操作Service实现
 * @createDate 2025-01-16 10:32:54
 */
@Slf4j
@Service
public class WmsBillsTypeBillsStatusServiceImpl implements WmsBillsTypeBillsStatusService
{

    @Autowired
    private WmsBillsTypeBillsStatusMapper wmsBillsTypeBillsStatusMapper;

    /**
     * 保存单据状态类型关联表
     *
     * @param entity
     * @return
     */
    @Override
    public AjaxResult saveBillsInfo(WmsBillsTypeBillsStatusEntity entity)
    {
        try {
            WmsBillsTypeBillsStatusEntity one = wmsBillsTypeBillsStatusMapper.isBillsExits(entity);

            if(! Objects.isNull(one)) return AjaxResult.success("已经保存");

            wmsBillsTypeBillsStatusMapper.saveBillsInfo(entity);
            return AjaxResult.success("保存成功");
        } catch(Exception e) {
            log.error("保存单据状态类型关联表失败", e);
            return AjaxResult.error("保存单据状态类型关联表失败");
        }
    }

    /**
     * 获取单据信息
     *
     * @return
     */
    @Override
    public R getBillsData()
    {
        // 获取有的单据状态
        List<WmsBillsTypeBillsStatusTreeVo> list = wmsBillsTypeBillsStatusMapper.findAllNccBillStatus();

        List<WmsBillsTypeBillsStatusTreeVo> collect = list.stream().map(item -> {
            item.setChildren(item.getChildren().parallelStream().filter(item1 -> item1.getEnableStatus().equals(1)).collect(Collectors.toList()));
            return item;
        }).collect(Collectors.toList());
        return R.ok(collect);
    }

    /**
     * 修改单据状态
     *
     * @param id
     * @return
     */
    @Override
    public R putBillsEnableStatus(Integer id)
    {
        try {
            wmsBillsTypeBillsStatusMapper.putBillsEnableStatus(id);
            return R.ok("修改成功");
        } catch(Exception e) {
            log.error("修改单据状态失败:{}", e);
            return R.fail("修改单据状态失败");
        }
    }
}




